Converted these images to PNG, saving a handful of bytes per image
[adiumx.git] / Utilities / Log Importers / gaim2adium.pl
blobd3c2c1875cb6d5f086aab33203389727ba547ba1
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use File::Find;
5 use File::Copy;
6 use Getopt::Long;
8 my $inDir = undef;
9 my $outDir = undef ;
10 my $adiumUser = undef;
11 my $force = 0;
12 my $skip = 0;
13 my $foundLogs = 0;
14 my $help = 0;
16 my %Protocols = ( #Map gaim protocol IDs to Adium ones
17 "aim" => "AIM",
18 "icq" => "ICQ",
19 "yahoo" => "Yahoo!",
20 "msn" => "MSN",
21 "jabber"=> "Jabber", # both jabber and gtalk
22 #Add the rest here, or tell me what they are, someone who uses other protocols
25 sub usage
27 my $msg = shift;
28 print "Error: $msg\n\n" if $msg;
29 print "Usage: gaim2adium.pl [--adiumUser <user> | --outDir <output dir>] [--inDir <input dir>] [--yes]\n";
30 print "Options: (defaults)\n\n";
31 print "\tinDir:\t\t\tDirectory to import logs from (~/.gaim/logs)\n";
32 print "\toutDir:\t\t\tDirectory to import logs to (./Logs)\n";
33 print "\tadiumUser:\t\tAttempt to automatically import logs to the specified Adium user\n";
34 print "\tforce\t\t\tOverwrite existing logs\n";
35 print "\tskip\t\t\tSkip existing logs\n";
36 print "\thelp:\t\t\tDisplay this help.\n";
37 print "\nOnce the logs have been imported, the contents of outDir can be dragged to your Adium log folder\n\n";
38 exit(defined $msg);
41 sub process_log
43 -r or return;
44 #gaim logs are LOG_BASE/Protocol/Account/Contact/YYYY-MM-DD-TIME.(html|txt)
45 if($File::Find::name =~ m! ^ $inDir # top level dir
46 (?:/)? # optional /
47 ([^/]+)/ # 1 - protocol
48 ([^/]+)/ # 2 - account
49 ([^/]+)/ # 3 - contact
50 (\d{4})- # 4 - year
51 (\d{2})- # 5 - month
52 (\d{2})\. # 6 - day
53 (\d{2}) # 7 - hour
54 (\d{2}) # 8 - minute
55 (\d{2}) # 9 - second
56 (?:-(\d{4})\w{3})? # 10 - optional timezone
57 \.(html|txt) # 11 - extension
58 !x)
60 my ($proto,$acct,$contact,$year,$month,$day,$hour,$min,$seconds,$tz,$ext) = ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11);
61 return unless defined ($proto = $Protocols{lc $proto});
62 if ($proto eq 'Jabber' and $acct =~ /gmail\.com/) {
63 $proto = "GTalk";
66 $foundLogs = 1; #Set the logs found flag
67 my $outFN = defined($tz) ? "$contact ($year-$month-${day}T$hour.$min.$seconds-$tz)." : "$contact ($year|$month|$day).";
68 $outFN .= ((lc $ext) eq "html") ? "html" : "adiumLog";
69 unless (-d "$outDir/$proto.$acct/$contact") {
70 unless (-d "$outDir/$proto.$acct") {
71 mkdir("$outDir/$proto.$acct") or die "Can't mkdir $outDir/$proto.$acct: $!";
73 mkdir("$outDir/$proto.$acct/$contact") or die "Can't mkdir $outDir/$proto.$acct/$contact: $!";
75 my $file = "$outDir/$proto.$acct/$contact/$outFN";
76 return if(-e $file && $skip);
77 if(-e $file && !$force)
79 #print(($adiumUser?"$adiumUser already has":"There already exists"),
80 #" a log from $proto.$acct to $contact on $day/$month/$year.\n");
81 `cat '$_' >> '$file'`;
82 } else {
83 copy($_,$file) or die "Failed to copy $_ to $file: $!";
85 `touch -t $year$month$day$hour$min.$seconds '$file'`;
89 #Sort a list of log files by time
90 sub sort_logs
92 my @files = @_;
93 return sort logcmp @files;
96 sub logcmp
98 my ($t1,$t2);
99 $t1 = $& if $a =~ /\d{6}/;
100 $t2 = $& if $b =~ /\d{6}/;
101 return 0 unless defined($t1) && defined($t2);
102 return $t1 <=> $t2;
106 GetOptions( "adiumUser=s" => \$adiumUser,
107 "inDir=s" => \$inDir,
108 "outDir=s" => \$outDir,
109 "yes" => \$force,
110 "force" => \$force,
111 "skip" => \$skip,
112 "help" => \$help)
113 or usage();
114 usage() if $help;
115 usage("You must supply at most one of adiumUser and outDir") if defined($outDir) && defined($adiumUser);
117 $outDir ||= "$ENV{HOME}/Library/Application Support/Adium 2.0/Users/$adiumUser/Logs" if defined $adiumUser;
118 $outDir ||= "$ENV{PWD}/Logs";
120 $outDir = "$ENV{PWD}/$outDir" unless $outDir =~ m!^/!;
122 $inDir ||= shift;
123 $inDir ||= "$ENV{HOME}/.gaim/logs";
125 print "NOTE: Output directory exists, existing logs will be appended to.\n" if(-d $outDir);
127 mkdir($outDir) unless -e $outDir;
128 usage("Output dir must be a directory") unless -d $outDir;
129 usage("Output dir must be writeable") unless -w $outDir;
131 usage("Input directory '$inDir' does not exist") unless -d $inDir;
132 usage("Input directory '$inDir' is not readable") unless -r $inDir;
134 #Spider the logs dir
135 find({wanted => \&process_log,
136 preprocess => \&sort_logs}, $inDir);
138 #Warn if we didn't find any logs
139 unless($foundLogs)
141 print "Warning: No recognized logs found.\n";
142 print "Note:\tThis script only supports logs generated by gaim 0.73 and above.\n";
143 print "\tYou may be able to update older gaim logs to the new format using the script from\n";
144 print "\thttp://sourceforge.net/forum/message.php?msg_id=2392758\n";
145 exit(1);
148 exit(0);